﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Threading.Tasks;
using System.Windows;
using Newtonsoft.Json;
using Prism.Ioc;
using Prism.Commands;
using Prism.Regions;
using WPFNote.Models;
using WPFNote.Services.IDal;
using WPFNote.Utilities.Common;
using WPFNote.Utilities.Extensions;
using WPFNote.Utilities.ReturnJson;
using SqlSugar;
using System.Drawing;
using System.Windows.Controls;

namespace WPFNote.ViewModels {
    public class IndexViewModel : NavigationViewModel {
        private readonly INote _note;
        private readonly IDialogHostService _dialogHost;
        private readonly IRegionManager _regionManager;
        public DelegateCommand QueryCommand { get; private set; }
        public DelegateCommand LoadMoreCommand { get; private set; }
        public DelegateCommand AddCommand { get; private set; }
        public DelegateCommand AddDayReportCommand { get; private set; }
        public DelegateCommand<Note> EditCommand { get; private set; }
        public DelegateCommand<Note> DeleteCommand { get; private set; }

        #region 属性
        private int _categoryId;

        public int CategoryId {
            get => _categoryId;
            set {
                _categoryId = value;
                RaisePropertyChanged();
            }
        }

        private int _page;

        public int Page {
            get => _page;
            set {
                _page = value;
                RaisePropertyChanged();
            }
        }

        private string _search;

        public string Search {
            get => _search;
            set {
                _search = value;
                RaisePropertyChanged();
            }
        }

        private List<Note> _notes;

        public List<Note> Notes {
            get => _notes;
            set {
                _notes = value;
                RaisePropertyChanged();
            }
        }

        /// <summary>
        /// 是否为空
        /// </summary>
        private Visibility _isEmpty;

        public Visibility IsEmpty {
            get => _isEmpty;
            set {
                _isEmpty = value;
                RaisePropertyChanged();
            }
        }

        /// <summary>
        /// 加载更多
        /// </summary>
        private Visibility _loadMore;

        public Visibility LoadMore {
            get => _loadMore;
            set {
                _loadMore = value;
                RaisePropertyChanged();
            }
        }

        #endregion

        public IndexViewModel(IContainerProvider containerProvider, IRegionManager regionManager, INote note) : base(
            containerProvider) {
            _note = note;
            QueryCommand = new DelegateCommand(Query);
            LoadMoreCommand = new DelegateCommand(LoadMoreList);
            AddCommand = new DelegateCommand(ToAdd);
            AddDayReportCommand = new DelegateCommand(ToAddAddDayReport);
            EditCommand = new DelegateCommand<Note>(ToEdit);
            DeleteCommand = new DelegateCommand<Note>(Delete);
            _dialogHost = containerProvider.Resolve<IDialogHostService>();
            _regionManager = regionManager;
        }

        private async void GetList() {
            RefAsync<int> total = 0;
            var sch = new Models.NoteSch()
            {
                Title = string.IsNullOrWhiteSpace(Search) ? null : Search,
                CategoryId = CategoryId == 0 ? null : CategoryId,
                UserId = AppSession.UserInfo.Id
            };
            var res = await _note.SelectListAsync(sch, Page, sch.Size, total);
            Notes = res;
            IsEmpty = Notes.Count > 0 ? Visibility.Hidden : Visibility.Visible;
            LoadMore = total > Notes.Count ? Visibility.Visible : Visibility.Collapsed;
        }

        private void Query() {
            Notes = new List<Note>();
            Page = 1;
            GetList();
        }

        private void LoadMoreList() {
            Page++;
            GetList();
        }

        private void ToAdd() {
            _regionManager.RequestNavigate(PrismManager.MainViewRegionName, "NoteAdd", new NavigationParameters() {
                {
                    "categoryId", CategoryId
                },
                {
                    "isDayReport", false
                }
            });
        }

        private void ToAddAddDayReport()
        {
            _regionManager.RequestNavigate(PrismManager.MainViewRegionName, "NoteAdd", new NavigationParameters() {
                {
                    "categoryId", CategoryId
                },
                {
                    "isDayReport", true
                }
            });
        }

        private void ToEdit(Note model) {
            _regionManager.RequestNavigate(PrismManager.MainViewRegionName, "NoteEdit", new NavigationParameters() {
                {
                    "id", model.Id
                }
            });
        }

        private async void Delete(Note model) {
            var dialogResult = await _dialogHost.Question("温馨提示", $"确认删除笔记:{model.Title} ?");
            if (dialogResult.Result != Prism.Services.Dialogs.ButtonResult.OK)
                return;
            var result = await _note.DeleteAsync(model.Id);
            if (result) {
                Aggregator.SendMessage("删除成功");
                Query();
            } else {
                Aggregator.SendMessage("删除失败");
            }
        }

        public override void OnNavigatedTo(NavigationContext navigationContext) {
            CategoryId = navigationContext.Parameters.GetValue<int>("id");
            Query();
        }
    }
}